package com.zx.web.dao.impl;

import com.zx.bean.Order;
import com.zx.bean.User;
import com.zx.utils.DataSourceUtils;
import com.zx.web.dao.AdminUserDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.dao.impl
 * @Description:
 * @Date 2022/9/16 9:44
 */
public class AdminUserDaoImpl implements AdminUserDao {
    @Override
    public User findAdmin(String username, String password) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_user where username = ? and password = md5(?)";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanHandler<>(User.class),username,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAllValid() {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_user where flag = 1 ";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(User.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteUser(int id) {
        QueryRunner qr = new QueryRunner();
        String sql = "delete from tb_user where id = ? ";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public List<User> findUserList(String username, String gender) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_user where username = ? and gender = ?";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(User.class),username,gender);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAllInValid() {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_user where flag = 2 ";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(User.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order> findOrder(int uid) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_order where uid = ? ";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(Order.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteOrderDetail(String id) {
        QueryRunner qr = new QueryRunner();
        String sql = "delete from tb_orderdetail where oid = ?";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void deleteOrder(int uid) {
        QueryRunner qr = new QueryRunner();
        String sql = "delete from tb_order where uid = ?";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void deleteCart(int id) {
        QueryRunner qr = new QueryRunner();
        String sql = "delete from tb_cart where id = ?";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void deleteAddress(int id) {
        QueryRunner qr = new QueryRunner();
        String sql = "delete from tb_address where uid = ?";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


}
